/**
 * @author blancsv@usi.ch
 * @author daniele.gili@usi.ch
 */

package ch.usi.inf.pf2;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.BoxLayout;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;

public class PanicSimFrameLeftItemBar extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private PanicSimMain circuit;
	private PanicSimDrawingArea area;
	private CommonGate selectedGate;
	private JPanel infoPanel;

	public PanicSimFrameLeftItemBar(final PanicSimMain circuit,
			final PanicSimDrawingArea area) {
		this.setCircuit(circuit);
		this.setArea(area);

		JPanel x = new JPanel();
		infoPanel = new PanicSimInfoPanel(circuit, area);
		BoxLayout f = new BoxLayout(x, BoxLayout.Y_AXIS);
		x.setLayout(f);

		// Buttons for the gates
		JButton buttonNot = new JButton();
		Icon iconNot = new ImageIcon(getClass().getResource("Icons/not.png"));
		buttonNot.setIcon(iconNot);
		buttonNot.setToolTipText("Not Gate");

		JButton buttonAnd = new JButton();
		Icon iconAnd = new ImageIcon(getClass().getResource("Icons/and.png"));
		buttonAnd.setIcon(iconAnd);
		buttonAnd.setToolTipText("And Gate");

		JButton buttonNand = new JButton();
		Icon iconNand = new ImageIcon(getClass().getResource("Icons/nand.png"));
		buttonNand.setIcon(iconNand);
		buttonNand.setToolTipText("Nand Gate");

		JButton buttonOr = new JButton();
		Icon iconOr = new ImageIcon(getClass().getResource("Icons/or.png"));
		buttonOr.setIcon(iconOr);
		buttonOr.setToolTipText("Or Gate");

		JButton buttonXor = new JButton();
		Icon iconXor = new ImageIcon(getClass().getResource("Icons/xor.png"));
		buttonXor.setIcon(iconXor);
		buttonXor.setToolTipText("Xor Gate");

		// Buttons for input and output and wire
		JButton buttonMi = new JButton();
		Icon iconMi = new ImageIcon(getClass().getResource("Icons/input.png"));
		buttonMi.setIcon(iconMi);
		buttonMi.setToolTipText("Input");

		JButton buttonMo = new JButton();
		Icon iconMo = new ImageIcon(getClass().getResource("Icons/ouput.png"));
		buttonMo.setIcon(iconMo);
		buttonMo.setToolTipText("Output");

		JButton buttonWire = new JButton();
		Icon iconWire = new ImageIcon(getClass().getResource("Icons/wire.png"));
		buttonWire.setIcon(iconWire);
		buttonWire.setToolTipText("Wire");

		// Buttons for selection
		JButton buttonSelect = new JButton();
		Icon iconSelect = new ImageIcon(getClass().getResource(
				"Icons/select.png"));
		buttonSelect.setIcon(iconSelect);
		buttonSelect.setToolTipText("Select");

		JButton buttonDelete = new JButton();
		Icon iconDelete = new ImageIcon(getClass().getResource(
				"Icons/delete.png"));
		buttonDelete.setIcon(iconDelete);
		buttonDelete.setToolTipText("Delete");

		// Button for running the circuit
		JButton buttonCompile = new JButton();
		Icon iconRun = new ImageIcon(getClass().getResource("Icons/run.png"));
		buttonCompile.setIcon(iconRun);
		buttonCompile.setToolTipText("Compile");

		// ActionListeners for all buttons
		
		buttonSelect.addActionListener(new ActionListener() {

			public void actionPerformed(final ActionEvent ev) {
				area.resetEnable();
				area.setSelectedGate(new ArrayList<Object>());
			}
		});
		
		
		buttonNot.addActionListener(new ActionListener() {

			public void actionPerformed(final ActionEvent ev) {
				area.resetEnable();
				area.setNot();
				area.setSelectedGate(new ArrayList<Object>());
			}
		});

		buttonMo.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent ev) {
				area.resetEnable();
				area.setMegaOutput();
				area.setSelectedGate(new ArrayList<Object>());
			}
		});

		buttonAnd.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent ev) {
				area.resetEnable();
				area.setAnd();
				area.setSelectedGate(new ArrayList<Object>());
			}
		});

		buttonCompile.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent ev) {
				
				area.setCompile();
				circuit.stopThread();
				area.refreshDrawer(0);
				if(area.getCompile()){
				area.setSelectedGate(new ArrayList<Object>());
				circuit.startCompile(area);
				new Thread(new Runnable() {
					public void run() {
						area.repaint();
					}
				});}
			}

		});

		buttonMi.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent ev) {
				area.resetEnable();
				area.setMegaInput();
				area.setSelectedGate(new ArrayList<Object>());
			}
		});

		buttonWire.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent ev) {
				area.resetEnable();
				area.setWire();
				area.setSelectedGate(new ArrayList<Object>());
			}
		});

		buttonDelete.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent ev) {
				if (area.getFocusedGate() != null) {
					area.resetEnable();

					for(int u = 0; u < area.getFocusedGate().size(); u++ ){
						
					for (int i = 0; i < ((CommonGate) circuit.getGate(area
							.getFocusedGate().get(u).getIndex())).getNumberInput(); i++) {

						if (((CommonGate) circuit.getGate(area.getFocusedGate().get(u)
								.getIndex())).getInputs().get(i).getInput() != null) {

							int f = ((CommonGate) circuit.getGate(area
									.getFocusedGate().get(u).getIndex())).getInputs()
									.get(i).getInput().getIndex();

							((CommonGate) circuit.getGate(area.getFocusedGate().get(u)
									.getIndex())).getInputs().get(i)
									.setInput(null);

							circuit.getpartList().set(f, null);
						}
					}}

					
					circuit.removePart(area.getFocusedGate());
					area.deleteFocusElement();

					area.repaint();
				}
			}
		});

		buttonNand.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent ev) {
				area.setSelectedGate(new ArrayList<Object>());
				area.resetEnable();
				area.setNand();
				System.out.println("added a Nand");
				area.repaint();
			}
		});

		buttonOr.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent ev) {
				area.setSelectedGate(new ArrayList<Object>());
				area.resetEnable();
				area.setOr();
				System.out.println("added a Or");
				area.repaint();
			}
		});

		buttonXor.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent ev) {
				area.setSelectedGate(new ArrayList<Object>());
				area.resetEnable();
				area.setXor();
				System.out.println("added a Xor");
				area.repaint();
			}
		});

		x.add(buttonNot);
		x.add(buttonAnd);
		x.add(buttonNand);
		x.add(buttonOr);
		x.add(buttonXor);
		x.add(buttonMi);
		x.add(buttonMo);
		x.add(buttonWire);
		x.add(buttonSelect);
		x.add(buttonDelete);
		x.add(buttonCompile);

		x.add(infoPanel, BorderLayout.SOUTH);

		// x.setBackground(new Color(100, 100, 100));
		x.setPreferredSize(new Dimension(200, 800));
		add(x, BorderLayout.SOUTH);
		// setBackground(new Color(200, 200, 200));

	}

	public void setCircuit(PanicSimMain circuit) {
		this.circuit = circuit;
	}

	public PanicSimMain getCircuit() {
		return circuit;
	}

	public void setArea(PanicSimDrawingArea area) {
		this.area = area;
	}

	public PanicSimDrawingArea getArea() {
		return area;
	}

	public void setSelectedGate(CommonGate selectedGate) {
		this.selectedGate = selectedGate;
	}

	public CommonGate getSelectedGate() {
		return selectedGate;
	}

	public JPanel getInfoPanel() {
		return infoPanel;
	}

	public void setInfoPanel(JPanel infoPanel) {
		this.infoPanel = infoPanel;
	}

}
